<?php

namespace App\Admin\Controllers;

use App\ChestType;
use App\Http\Controllers\Controller;
use App\Package;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ChestTypeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('电柜类型管理')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('电柜类型管理')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('电柜类型管理')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('电柜类型管理')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ChestType);

        $grid->id('ID');
        //通过get参数 实现关联查询
        $chestNO = request()->get('type_name');

        if ($chestNO) {
            $grid->model()->where(function ($query) use ($chestNO) {
                $query->whereHas('chestType', function ($query) use ($chestNO) {
                    $query->where('type_name', 'like', '%' . $chestNO . '%');
                });

            }, '所属套餐');
        }

        $grid->type_name('类型名称');
        $grid->package_id('绑定套餐名称')->display(function ($value) {
            $text = '';
            $packages = Package::whereIn('id', $value)->get();
            foreach ($packages as $package) {
                $route = route('package.index', ['package_name' => $package->package_name]);
                $text .= '<a href="' . $route . '">' . $package->package_name . '</a>';
                $text .= '<br>';
            }
            return $text;

        });
        $grid->created_at('添加时间');
        $grid->updated_at('编辑时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ChestType::findOrFail($id));

        $show->id('ID');
        $show->type_name('类型名称');
        $show->package_id('绑定套餐名称')->display(function ($value) {
            $text = '';
            $packages = Package::whereIn('id', $value)->get();
            foreach ($packages as $package) {
                $text .= '<p>' . $package->package_name . '</p>';
                $text .= '<br>';
            }
            return $text;

        });
        $show->created_at('添加时间');
        $show->updated_at('编辑时间');
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ChestType);
        $form->text('type_name', '类型名称');
        $form->multipleSelect('package_id', '绑定套餐')->options(Package::pluck('package_name', 'id'));
        return $form;
    }
}
